viewer = new Cesium.Viewer('cesiumContainer',{
    skyBox : false,
    skyAtmosphere : false,
    baseLayerPicker : false
});
document.getElementsByClassName("cesium-infoBox-iframe")[0].sandbox="allow-same-origin allow-popups allow-forms allow-scripts";
var canvas = viewer.canvas;
canvas.setAttribute('tabindex', '0'); // needed to put focus on the canvas
canvas.onclick = function() {
    canvas.focus();
};
var scene = viewer.scene;
var ellipsoid = viewer.scene.globe.ellipsoid; 
scene.imageryLayers.removeAll(true);
scene.globe.baseColor = new Cesium.Color(0.0, 0.0, 0.0,0.0); // 没有影像时地球的基础颜色，默认为蓝色
scene.globe.enableLighting=false;
scene.globe.depthTestAgainstTerrain=false;
scene.globe.showGroundAtmosphere=false;
$(".cesium-viewer-toolbar,.cesium-viewer-animationContainer,.cesium-viewer-timelineContainer,.cesium-viewer-bottom").css("display","none");
$(".cesium-viewer-fullscreenContainer .cesium-button").css("background","#000");
var selectedEntity = new Cesium.Entity(); 

viewer.entities.add({
    name : "巷道",
    polylineVolume : {
        positions : Cesium.Cartesian3.fromDegreesArrayHeights([ 116.39000393372546, 39.907459990947426, 10050,
                                                                116.47080393372546, 39.907459990947426, 10050]),
        shape : [   new Cesium.Cartesian2(-195, -50),
                    new Cesium.Cartesian2(195, -50),
                    new Cesium.Cartesian2(150, 240),
                    new Cesium.Cartesian2(-150, 240)],
        outline : true,
        outlineColor : Cesium.Color.BLACK,
        material : Cesium.Color.fromBytes(120, 120, 120, 130)
    }       
});
viewer.camera.lookAt(Cesium.Cartesian3.fromDegrees(	116.43014253508332, 39.90737320364527, 10301.7579591073467), new Cesium.Cartesian3(-9000, 9000, 9000));

var dataJson = {
    "001采集孔":{
        "bottom":2.9,
        "left":0,
        "start":0,
        "wall":"left",
        "name":"001采集孔",
        "angle":-50,
        "dataList":{  //模拟的数据形式
            "煤_1":[0,30],
            "水_1":[30,50],
            "石头_1":[40,70],
            "煤_2":[70,80],
            }  
    },
    "0011采集孔":{
        "bottom":2.9,
        "left":0,
        "start":0.15,
        "wall":"left",
        "name":"001采集孔",
        "angle":-50,
        "dataList":{  //模拟的数据形式
            "煤_1":[0,30],
            "水_1":[30,50],
            "石头_1":[40,70],
            "煤_2":[70,80],
            }  
    },
    "0012采集孔":{
        "bottom":2.9,
        "left":0,
        "start":0.30,
        "wall":"left",
        "name":"001采集孔",
        "angle":-50,
        "dataList":{  //模拟的数据形式
            "煤_1":[0,30],
            "水_1":[30,50],
            "石头_1":[40,70],
            "煤_2":[70,80],
            }  
    },
    "0013采集孔":{
        "bottom":2.9,
        "left":0,
        "start":0.45,
        "wall":"left",
        "name":"001采集孔",
        "angle":-50,
        "dataList":{  //模拟的数据形式
            "煤_1":[0,30],
            "水_1":[30,50],
            "石头_1":[40,70],
            "煤_2":[70,80],
            }  
    },
    "0014采集孔":{
        "bottom":2.9,
        "left":0,
        "start":0.60,
        "wall":"left",
        "name":"001采集孔",
        "angle":-50,
        "dataList":{  //模拟的数据形式
            "煤_1":[0,30],
            "水_1":[30,50],
            "石头_1":[40,70],
            "煤_2":[70,80],
            }  
    },
    "0015采集孔":{
        "bottom":2.9,
        "left":0,
        "start":0.75,
        "wall":"left",
        "name":"001采集孔",
        "angle":-50,
        "dataList":{  //模拟的数据形式
            "煤_1":[0,30],
            "水_1":[30,50],
            "石头_1":[40,70],
            "煤_2":[70,80],
            }  
    },
    "0016采集孔":{
        "bottom":2.9,
        "left":0,
        "start":0.90,
        "wall":"left",
        "name":"001采集孔",
        "angle":-50,
        "dataList":{  //模拟的数据形式
            "煤_1":[0,30],
            "水_1":[30,50],
            "石头_1":[40,70],
            "煤_2":[70,80],
            }  
    },
    "0017采集孔":{
        "bottom":2.9,
        "left":0,
        "start":1.05,
        "wall":"left",
        "name":"001采集孔",
        "angle":-50,
        "dataList":{  //模拟的数据形式
            "煤_1":[0,30],
            "水_1":[30,50],
            "石头_1":[40,70],
            "煤_2":[70,80],
            }  
    },
    "0018采集孔":{
        "bottom":2.9,
        "left":0,
        "start":1.20,
        "wall":"left",
        "name":"001采集孔",
        "angle":-50,
        "dataList":{  //模拟的数据形式
            "煤_1":[0,30],
            "水_1":[30,50],
            "石头_1":[40,70],
            "煤_2":[70,80],
            }  
    },
    "0019采集孔":{
        "bottom":2.9,
        "left":0,
        "start":1.35,
        "wall":"left",
        "name":"001采集孔",
        "angle":-50,
        "dataList":{  //模拟的数据形式
            "煤_1":[0,30],
            "水_1":[30,50],
            "石头_1":[40,70],
            "煤_2":[70,80],
            }  
    },
    "002采集孔":{
        "bottom":2.9,
        "left":0,
        "start":40,
        "wall":"left",
        "name":"002采集孔",
        "angle":30,
        "dataList":{  //模拟的数据形式
            "煤_1":[0,30],
            "水_1":[30,50],
            "石头_1":[40,70],
            }  
    },
    "003采集孔":{
        "bottom":0,
        "left":0,
        "start":80,
        "wall":"left",
        "name":"003采集孔",
        "angle":30,
        "dataList":{  //模拟的数据形式
            "煤_1":[0,30],
            "水_1":[30,40],
            "石头_1":[40,70],
            }  
    },
    "004采集孔":{
        "bottom":0,
        "left":1,
        "start":0,
        "wall":"top",
        "name":"004采集孔",
        "angle":90,
        "dataList":{  //模拟的数据形式
            "煤_1":[0,30],
            "水_1":[30,50],
            "石头_1":[40,70],
            }  
    },
    "005采集孔":{
        "bottom": 0,
        "left":0.5,
        "start":40,
        "wall":"top",
        "name":"005采集孔",
        "angle":60,
        "dataList":{  //模拟的数据形式
            "煤_1":[0,30],
            "水_1":[30,50],
            "石头_1":[40,70],
            }  
    },
    "006采集孔":{
        "bottom":1.5,
        "left":0,
        "start":80,
        "wall":"right",
        "name":"006采集孔",
        "angle":150,
        "dataList":{  //模拟的数据形式
            "煤_1":[50,70],
            "水_1":[0,50],
            "石头_1":[40,70],
            }  
    }
}

var addHandHole = function(data){//bottom,left,start,wall,name

    let height = 2.90;
    let width = 0.5;
    let tan = height/width;
    if(data.wall != "top"){
        if(data.wall == "left"){
            let left = data.bottom / tan;
            addButton(viewer,116.38995464596081 + 0.00101*data.start,39.909204434602684 - left * 0.00086 ,10058.62427878900178 + 93.89683143187872 * data.bottom,data.name,data.dataList,data.angle);
        }else if(data.wall == "right"){
            let right = data.bottom / tan;
            addButton(viewer,116.38995464596081 + 0.00101*data.start,39.90578505453292 + right * 0.00086 ,10058.62427878900178 + 93.89683143187872 * data.bottom,data.name,data.dataList,data.angle);
        }
    }else{
        addButton(viewer,116.38995464596081 + 0.00101*data.start,39.908825389827214 - data.left * 0.00086 ,10338.3686263741616,data.name,data.dataList,data.angle);
    }
}

for(let i in dataJson){
    addHandHole(dataJson[i]);
}

function addButton(viewer,lon,lat,ele,name,data,angle){   	   
    var cam = viewer.entities.add({
        name: name,
        position: Cesium.Cartesian3.fromDegrees(lon, lat, ele),
        //orientation: orientation,
        ellipsoid : {
            radii : new Cesium.Cartesian3(5.0, 5.0, 5.0),
            material : Cesium.Color.AQUA.withAlpha(1)
        }
    });

    for(let i in data){
        if(i.slice(0,1) == "煤"){
            addEllipses(lon,lat,ele,i,data,Cesium.Color.fromBytes(150, 150, 150, 150),angle)
        }else if(i.slice(0,2) == "石头"){
            addEllipses(lon,lat,ele,i,data,Cesium.Color.fromBytes(155, 128, 57, 150),angle)
        }else if(i.slice(0,1) == "水"){
            addEllipses(lon,lat,ele,i,data,Cesium.Color.fromBytes(0, 200, 255, 150),angle)
        }
    }
}


function addEllipses(lon,lat,ele,i,data,color,angle){
    //console.log(angle,Math.sin(angle),Math.cos(angle));
    angle = (angle+270) * Math.PI / 180;
    viewer.entities.add({
        name : i,
        polylineVolume : {
            positions : Cesium.Cartesian3.fromDegreesArrayHeights([ 
                lon , lat - 0.000738*data[i][0]*Math.sin(angle), ele - (0.15*93.89683143187872)+(93.89683143187872*data[i][0]*Math.cos(angle)),
                lon , lat - 0.000738*data[i][1]*Math.sin(angle), ele - (0.15*93.89683143187872)+(93.89683143187872*data[i][1]*Math.cos(angle))
            ]),
            shape : [   new Cesium.Cartesian2(-12.931, -12.931),
                        new Cesium.Cartesian2(12.931, -12.931),
                        new Cesium.Cartesian2(12.931, 12.931),
                        new Cesium.Cartesian2(-12.931, 12.931)],
            
            material : color
        }       
    });
}

viewer.camera.changed.addEventListener(function(percentage) {

}); 

function cartesianToWGS84(x,y,z){
    let ellipsoid=viewer.scene.globe.ellipsoid;
    let cartesian3=new Cesium.Cartesian3(x,y,z);
    let cartographic = ellipsoid.cartesianToCartographic(cartesian3);
    let lat = Cesium.Math.toDegrees(cartographic.latitude);
    let lon = Cesium.Math.toDegrees(cartographic.longitude);
    let ele = cartographic.height;
    return {lon,lat,ele}
}

var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);//屏蔽双击事件

var selectedEntity = new Cesium.Entity(); 

viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(movement) {
    var pickedFeature = viewer.scene.pick(movement.position);    
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

handler.setInputAction(function(wheelment){
     
},Cesium.ScreenSpaceEventType.WHEEL);

